//package cn.flying.cloud.base.loadbalancer;
//
//import cn.flying.cloud.base.common.utils.Ft;
//import com.alibaba.cloud.nacos.discovery.NacosDiscoveryClient;
//import org.springframework.cloud.client.ServiceInstance;
//import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
//import org.springframework.cloud.client.loadbalancer.LoadBalancerRequest;
//import org.springframework.cloud.loadbalancer.blocking.client.BlockingLoadBalancerClient;
//import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
//
//import java.io.IOException;
//import java.net.URI;
//import java.util.List;
//import java.util.Optional;
//
///**
// * 自定义本地负载均衡策略
// *
// * @author: admin
// * @date: 2025年03月08日 15:10
// * @version: 1.0
// */
//public class CustomLoadBalancerClient implements LoadBalancerClient {
//
//    private final LoadBalancerClient defaultLoadBalancerClient;
//    private final NacosDiscoveryClient discoveryClient;
//
//    public CustomLoadBalancerClient(LoadBalancerClientFactory loadBalancerClientFactory, NacosDiscoveryClient nacosDiscoveryClient) {
//        // 手动创建默认的BlockingLoadBalancerClient实例
//        this.defaultLoadBalancerClient = new BlockingLoadBalancerClient(loadBalancerClientFactory);
//        this.discoveryClient = nacosDiscoveryClient;
//    }
//
//    @Override
//    public <T> T execute(String serviceId, LoadBalancerRequest<T> request) throws IOException {
//        String requestId = Ft.Local.RequestId();
//        String envId = Ft.Local.EnvId();
//        ServiceInstance instance = choose(serviceId, envId, requestId);
//        if (instance == null) {
//            instance = defaultLoadBalancerClient.choose(serviceId);
//        }
//        return defaultLoadBalancerClient.execute(serviceId, instance, request);
//    }
//
//    @Override
//    public <T> T execute(String serviceId, ServiceInstance serviceInstance, LoadBalancerRequest<T> request) throws IOException {
//        return defaultLoadBalancerClient.execute(serviceId, serviceInstance, request);
//    }
//
//    @Override
//    public ServiceInstance choose(String serviceId) {
//        return defaultLoadBalancerClient.choose(serviceId);
//    }
//
//    private ServiceInstance choose(String serviceId, String envId, String requestId) {
//        // 空值则使用默认的LoadBalancerClient
//        if (Ft.String.isBlank(envId) || Ft.String.isBlank(requestId)) {
//            return defaultLoadBalancerClient.choose(serviceId);
//        }
//        List<ServiceInstance> instances = discoveryClient.getInstances(serviceId);
//        Optional<ServiceInstance> optional = instances.stream()
//                .filter(instance -> {
//                    String metaRequestId = instance.getMetadata().get("request-id");
//                    String metaEnvId = instance.getMetadata().get("env-id");
//                    return envId.equals(metaEnvId) && requestId.equals(metaRequestId);
//                }).findFirst();
//        return optional.orElseGet(() -> defaultLoadBalancerClient.choose(serviceId));
//    }
//
//    @Override
//    public URI reconstructURI(ServiceInstance instance, URI original) {
//        return defaultLoadBalancerClient.reconstructURI(instance, original);
//    }
//}